package cn.iocoder.yudao.module.scrm.service.schedules;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesPageReqVO;
import cn.iocoder.yudao.module.scrm.controller.admin.schedules.vo.SchedulesSaveReqVO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.customer.CustomerDO;
import cn.iocoder.yudao.module.scrm.dal.dataobject.schedules.SchedulesDO;
import cn.iocoder.yudao.module.scrm.dal.mysql.schedules.SchedulesMapper;
import cn.iocoder.yudao.module.scrm.dal.mysql.schedulesteacher.SchedulesTeacherMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.scrm.enums.ErrorCodeConstants.SCHEDULES_NOT_EXISTS;

/**
 * 排期 Service 实现类
 *
 * @author admin
 */
@Service
@Validated
public class SchedulesServiceImpl implements SchedulesService {

    @Resource
    private SchedulesMapper schedulesMapper;

    @Resource
    private SchedulesTeacherMapper schedulesTeacherMapper;

    @Resource
    private SchedulesTeacherService schedulesTeacherService;

    @Override
    @Transactional
    public Long createSchedules(SchedulesSaveReqVO createReqVO) {
        // 插入
        SchedulesDO schedules = BeanUtils.toBean(createReqVO, SchedulesDO.class);
        schedulesMapper.insert(schedules);
        schedulesTeacherService.createSchedulesTeacher(schedules.getId(), createReqVO.getTeacherList());
        // 返回
        return schedules.getId();
    }

    @Override
    public void updateSchedules(SchedulesSaveReqVO updateReqVO) {
        // 校验存在
        validateSchedulesExists(updateReqVO.getId());
        // 更新
        SchedulesDO updateObj = BeanUtils.toBean(updateReqVO, SchedulesDO.class);
        schedulesMapper.updateById(updateObj);
    }

    @Override
    public void deleteSchedules(Long id) {
        // 校验存在
        validateSchedulesExists(id);
        // 删除
        schedulesMapper.deleteById(id);
    }

    private void validateSchedulesExists(Long id) {
        if (schedulesMapper.selectById(id) == null) {
            throw exception(SCHEDULES_NOT_EXISTS);
        }
    }

    @Override
    public SchedulesDO getSchedules(Long id) {
        return schedulesMapper.selectById(id);
    }

    @Override
    public PageResult<SchedulesDO> getSchedulesPage(SchedulesPageReqVO pageReqVO) {
        return schedulesMapper.selectPage(pageReqVO);
    }

    @Override
    public IPage<CustomerDO> getCustomerList(SchedulesPageReqVO pageReqVO) {
        IPage<CustomerDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        return schedulesMapper.queryScheduleCustomer(page,pageReqVO.getId(),  pageReqVO.getCreator());
    }

}